params=struct();
tfinal=4;
params.t=linspace(0,tfinal,100);
params.delta=rand(size(params.t));
% params.delta=sin(params.t/pi));
opt=odeset("AbsTol",1e-4,"RelTol",1e-4,"OutputFcn","odeplot");
[t,x]=ode45(@(t,x)rhs(t,x,params),[0 tfinal],[1;0],opt);

figure;
nexttile;hold on;
plot(t,x(:,1));
nexttile;hold on;
plot(t,x(:,2));
plot(params.t,params.delta)
legend(["k","\delta"])
exportgraphics(gcf,"adaptive_sign.png")


function [dxdt]=rhs(t,states,p)
    x=states(1);
    k=states(2);
    u=-k*sign(x);
    delta=interp1(p.t,p.delta,t);
    dxdt=[u+delta;abs(x)];
end